package com.fingard.dsp.bank.directbank.cgbunion01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.text.StringHelper;

import java.util.HashMap;
import java.util.Map;

public class CGB01UnifiedOrder extends CGBUnion01Base{
    public RetAtsQueryOrder queryUnifiedOrder(ReqQueryOrder p_req) {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_req.rdSeq;
        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
            if (actItem != null) {
                Map<String, String> headMap = buildHeaderParam("findOrderJH",actItem);
                Map<String, Object> bodyMap = new HashMap<String, Object>();
                bodyMap.put("service", "unified.trade.query");
                bodyMap.put("mch_id", actItem.merchantId);//商户appId
                bodyMap.put("out_trade_no", p_req.rdSeq);
                bodyMap.put("nonce_str", get_nonce_str());
                bodyMap.put("sign", "asdf");
                JSONObject qryOrder  = new JSONObject();
                qryOrder.put("Header",headMap);
                qryOrder.put("Body",bodyMap);
                String returnStr = send(qryOrder,"findOrderJH",actItem);
                JSONObject resultObj = JSONObject.parseObject(returnStr);
                JSONObject resBody = resultObj.getJSONObject("Body");
                String result_code = resBody.getString("result_code");
                String err_code = resBody.getString("errorCode");
                String err_msg = resBody.getString("errorMsg");
                if(StringHelper.hasAnyChar(err_code)){
                    retAts.respCode = "-1";
                    retAts.respInfo = err_msg;
                }else {
                    retAts.respCode = "0";
                    if("0".equals(result_code)){
                        String trade_state = resBody.getString("trade_state");
                        retAts.respInfo = "success";
                        retAts.outTransID  = p_req.outTransID;
                        retAts.transState = trade_state;
                        retAts.payInfoCode = trade_state;
                    }
                }

            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
